package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.RatingBar;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessMerchant;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MerchantInfoVO;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.DateUtil;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/9 10:37
 * 修改备注：
 * ================================================
 */

public class MerchantsIntroduceActivity extends BaseActivity {

    @Bind(R.id.merchant_info_iv_avatar)
    SimpleDraweeView merchantInfoIvAvatar;
    @Bind(R.id.merchant_info_tv_merchant_name)
    TextView merchantInfoTvMerchantName;
    @Bind(R.id.merchant_info_rating_grade_level)
    RatingBar merchantInfoRatingGradeLevel;
    @Bind(R.id.merchant_info_tv_evaluate)
    TextView merchantInfoTvEvaluate;
    @Bind(R.id.merchant_info_tv_address)
    TextView merchantInfoTvAddress;
    @Bind(R.id.merchant_info_tv_register_date)
    TextView merchantInfoTvRegisterDate;
    @Bind(R.id.merchant_info_tv_contacts)
    TextView merchantInfoTvContacts;
    @Bind(R.id.merchant_info_tv_mobile_number)
    TextView merchantInfoTvMobileNumber;
    @Bind(R.id.merchant_info_tv_publish_data)
    TextView merchantInfoTvPublishData;
    @Bind(R.id.merchant_info_tv_orders)
    TextView merchantInfoTvOrders;

    private MerchantInfoVO merchantInfoVO;
    private String commodityCount; //  商品总数
    private String orderCount; //  累计订单

    @Override
    public void setLayout() {
        setTitle("商户简介");
        setContentLayout(R.layout.activity_merchants_introduce);
    }

    @Override
    public void initMember() {
        //TODO 测试 调用商户主页接口 应该从商户主页直接传递过来
        if (getIntent().hasExtra("merchantId")) {
            BusinessMerchant.showMerchantHome(this, (String) getIntent().getSerializableExtra("merchantId"), mHandler);
        }

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SHOW_MERCHANT_HOME_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SHOW_MERCHANT_HOME_FIELD:
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        merchantInfoVO = FastJsonUtil.getBean(object.toString(), "body", "merchantInfo", MerchantInfoVO.class);
        commodityCount = FastJsonUtil.getString(object.toString(), "commodityCount");
        orderCount = FastJsonUtil.getString(object.toString(), "orderCount");
        setContent();
    }

    /**
     * 填充数据
     */
    private void setContent() {
        FrescoUtil.showImageSmall(merchantInfoVO.getHeadPortrait(), merchantInfoIvAvatar); // 商户头像
        merchantInfoTvMerchantName.setText(merchantInfoVO.getMerchantName()); // 商户名
        if (merchantInfoVO == null || StringUtil.isStrEmpty(merchantInfoVO.getMerchantGrade())) {
            merchantInfoRatingGradeLevel.setNumStars(0);// 商户等级
        } else {

            int creditLevel = merchantInfoVO.getCreditLevel();
            if (creditLevel < 400)
                merchantInfoRatingGradeLevel.setNumStars(1);// 商户等级
            else if (400 <= creditLevel && creditLevel < 1600)
                merchantInfoRatingGradeLevel.setNumStars(2);// 商户等级
            else if (1600 <= creditLevel && creditLevel < 3600)
                merchantInfoRatingGradeLevel.setNumStars(3);// 商户等级
            else if (3600 <= creditLevel && creditLevel < 6400)
                merchantInfoRatingGradeLevel.setNumStars(4);// 商户等级
            else if (6400 <= creditLevel)
                merchantInfoRatingGradeLevel.setNumStars(5);// 商户等级
        }
        merchantInfoTvEvaluate.setText(String.format("%s分", StringUtil.isStrEmpty(merchantInfoVO.getMerchantGrade()) ? "" : merchantInfoVO.getMerchantGrade())); // 评价
        merchantInfoTvAddress.setText(merchantInfoVO.getFullAddress()); // 所在地
        merchantInfoTvRegisterDate.setText(DateUtil.StringToString(merchantInfoVO.getCreateTime(), DateStyle.YYYY_MM_DD_EN)); // 注册日期
        merchantInfoTvContacts.setText(merchantInfoVO.getTrueName()); // 联系人
        merchantInfoTvMobileNumber.setText(merchantInfoVO.getMobilePhone()); //售后电话
        merchantInfoTvPublishData.setText(commodityCount != null ? commodityCount : ""); // 发布商品
        merchantInfoTvOrders.setText(orderCount != null ? orderCount : ""); // 累计订单
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
